package com.jugheadzhou.cilent1.service;

import com.jugheadzhou.core.zk.helper.MythServiceRegistryHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author jugheadzhou
 * @date 2022-04-10
 */
@Slf4j
@Service
public class LockService1 {
    @Resource
    private MythServiceRegistryHelper serviceRegistryHelper;

    /**
     * 测试zookeeper lock
     * @param lockKey
     * @throws InterruptedException
     */
    public void lockTest(String lockKey) {
        log.info("start using zookeeper lock: {}", lockKey);
        serviceRegistryHelper.lock(lockKey);
        log.info("success using zookeeper lock: {}", lockKey);
        try {
            log.info("do something for zookeeper lock: {}", lockKey);
            TimeUnit.SECONDS.sleep(5);
        }catch (Exception e){
            log.error("处理业务发生未知异常", e);
        }finally {
            log.info("end using zookeeper lock: {}", lockKey);
            serviceRegistryHelper.unlock();
        }
    }

}
